<?php
declare (strict_types = 1);

namespace app\common\model;

use app\common\traits\ReModel;
use app\common\traits\AddEdit;
use think\Model;

class CommonBaseModel extends Model
{
    use ReModel;

    /**
     * 默认主键是id
     * 静态调用方法: 例 User::add()
     */
    public function addData($data)
    {   
        $pk = $this->getPk();

        // 不要带id
        unset($data[$pk]);

        $obj = self::create($data);
        if (isset($obj->id)) {
            return $obj->id;
        }

        return false;
    }

    /**
     * 默认主键是id
     * 静态调用方法: 例 User::edit()
     */
    public function editData($where = null, $data = null)
    {   
        $pk = $this->getPk();

        if ($data === null) {
            // 不要带id
            if ( ! empty($where[$pk])) {
                $data = $where;
                $where = null;
                $where[$pk] = $data[$pk];
                
                // 去掉主键
                unset($data[$pk]);
            }
        }else {
            if (is_numeric($where)) {
                $w = [];
                $w[$pk] = $where;
                $where = $w;
            }
        }

        if (empty($where) || empty($data) || !is_array($where) || !is_array($data)) {
            return false;
        }

        return self::update($data, $where);
    }
}
